<template>
	<view class="item rtl" v-for="(item,i) in $attrs.cartListData">
		<view class="num">
			#{{i+1}}
		</view>
		<view class="item_box" @click="getDetails(item.id,i)">
			<view class="item_text">
				<view class="info">
					<view class="info_num">
						{{item.oe}}
					</view>
					<view class="info_price">
						{{item.productPrice}}SAR
					</view>
				</view>
				<view class="notes">
					{{item.productName}}
				</view>
			</view>
			<view class="item_btn">
				{{item.quantity}}
			</view>
		</view>

	</view>

	<Model ref="goodsModel" :title="title" @Confrm="goodsConfrm">
		<view class="" v-if="goodsDetail">
			<swiper v-if="goodsDetail.pic" class="swiper" circular indicator-dots>
				<swiper-item v-for="item in goodsDetail.pic">
					<image :src="item" mode="widthFix"></image>
				</swiper-item>
			</swiper>
			<view class="p_name">
				<view class="img">
				</view>
				<view class="">
					<view class="">
						{{goodsDetail.productName}}
					</view>
					<view class="good-year">
						{{goodsDetail.year}}
					</view>
				</view>
			</view>
			<view class="goods-num">
				<view class="goods-sum" @click="()=>{num>1?num--:''}">
					-
				</view>
				<view class="input">
					{{num}}
				</view>
				<view class="goods-sum" @click="()=>{num<goodsDetail.stock?num++:''}">
					+
				</view>
			</view>

		</view>
	</Model>
	<Model ref="addressModel" :show="false" title="Address&Remark" @Confrm="addressConfrm" modelStyle="minHeight:70%">
		<view class="rtl">
			<view class="address-title">
				<view class="">
					{{t("Delivery to")}}
				</view>
				<view class="" @click="goToView('/pages/user/addressDeit')">
					<image class="icon_img" src="/static/images/detail.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="address_info">
				<view class="address_img">
					<image class="img" src="/static/log.png" mode="aspectFit"></image>
				</view>
				<view class="address_info_detail">
					<view class="postCode"> {{addressInfo.StoreName}} </view>
					<view class="address_info_item">
						{{ t("consignee") }}: {{addressInfo.name}}
					</view>
					<view class="address_info_item"> {{ t("phone") }}:{{addressInfo.phone}} </view>
					<view class="address_info_item">
						{{ t("address") }}：{{addressInfo.address}}
					</view>
				</view>
			</view>
			<view class="remark">
				<view class="remark-title">
					{{t("Remark")}}
				</view>
				<textarea class="remark-text" v-model="remark"></textarea>
			</view>
		</view>
	</Model>
	<shopCartPlace ref="CartPlace" />
</template>

<script setup>
	import shopCartPlace from "@/components/shopCartView/shopCartPlace.vue"
	import {
		ref,
		useAttrs,
		onMounted,
		defineExpose,
		defineEmits
	} from "vue"
	const $attrs = useAttrs();
	import {
		CartAPI,
		OrderAPI
	} from "@/api/index";
	import {
		onShow
	} from "@dcloudio/uni-app"
	import {
		useI18n
	} from "vue-i18n";
	import {
		useAuthStore,
		useDeviceStore
	} from "@/store";
	const deviceStore = useDeviceStore()
	const {
		t
	} = useI18n()
	const emits = defineEmits(["computedPriceBack"]);
	const dataList = ref([])

	const goodsModel = ref()
	const addressModel = ref()
	const CartPlace = ref()
	const num = ref(1)
	const title = ref()
	const search_text = ref("")
	const remark = ref("")
	const orderDetailList = ref([])
	const TotalPrice = ref(0)
	const TotalNum = ref(0)
	const addressInfo = ref({})
	const goodsDetail = ref(null)
	const goodsIndex = ref(-1)
	const getDetails = async (id, index) => {
		goodsIndex.value = index;
		let res = await CartAPI.getDetails(id);
		// console.log(goodsModel.value)
		// title.value = res.oe
		goodsModel.value.control(true)
		goodsDetail.value = res
		// console.log(res)
	}
	onMounted(() => {
		dataList.value = $attrs.cartListData
		countTotalPrice()
	})
	const goToView = (path) => {
		uni.navigateTo({
			url: path
		})
	}
	onShow(() => {
		let address = uni.getStorageSync("address");
		if (address) {
			addressInfo.value = address;
		}
	})
	const goodsConfrm = async () => {
		orderDetailList.value.push({
			"oe": goodsDetail.value.oe,
			"quantity": num.value
		})
		console.log(dataList.value, goodsIndex.value)
		const item = dataList.value[goodsIndex.value]
		item.quantity = num.value;
		countTotalPrice()
		num.value = 1;
		goodsModel.value.control(false)

	}
	const countTotalPrice = () => {

		TotalPrice.value = 0
		dataList.value.forEach(items => {
			TotalPrice.value = (Number(TotalPrice.value) + (items.productPrice * items.quantity)).toFixed(2)
			TotalNum.value = Number(TotalNum.value) + items.quantity
		})

		computedPrice()
	}
	const Checkout = () => {
		if (orderDetailList.value.length > 0) {
			let arr = []
			dataList.value.forEach(item => {
				if (item.quantity > 0) {
					arr.push({
						"oe": item.oe,
						"quantity": item.quantity
					})
				}
			})
			if (arr.length > 0) {
				orderDetailList.value = arr
			}
			addressModel.value.control(true)
		}
	}
	const addressConfrm = async () => {
		addressModel.value.control(false)
		let res = await OrderAPI.postPlaceOrder({
			"shopId": deviceStore.MyShop.id,
			"paymentMethod": deviceStore.MyShop.paymentMethod, // 0现结 1账期
			"orderType": "0", //0->正常订单；1->促销订单
			"remark": remark.value, //备注
			"orderDetailList": orderDetailList.value,
			//  [{
			// 	"oe": "", //oe号
			// 	"quantity": 1 //商品个数
			// }],
			"orderReceiverAddressDto": {
				"receiverName": addressInfo.value.name, //收货人姓名
				"receiverPhone": addressInfo.value.phone, //收货人电话
				// "receiverProvince": "省份/直辖市", //省份/直辖市
				// "receiverCity": "城市", //城市
				// "receiverRegion": "区", //区
				"receiverDetailAddress": addressInfo.value.address //详细地址
			}
		})
		console.log(res)
		CartPlace.value.control(true)
	}
	const computedPrice = () => {

		emits("computedPriceBack", {
			totalQuantity: TotalNum.value,
			totalProductPrice: TotalPrice.value
		})

	}
	defineExpose({
		Checkout
	})
</script>

<style lang="scss" scoped>
	@import "@/static/css/index.scss";

	.item {
    background-color: red;
		margin-bottom: 20rpx;

		.num {
			color: #7f7f7f;
			font-size: 24rpx;
			margin-left: 5rpx;
		}

		.item_box {
			display: flex;
			align-items: center;
			background: #fff;
			border-radius: 10rpx;
			padding: 20rpx;

			.item_text {
				flex: 1;

				.info {
					display: flex;
					align-items: center;

					.info_num {
						font-size: 32rpx;
					}

					.info_price {
						color: #E57854;
						font-size: 24rpx;
					}
				}

				.notes {
					margin-top: 10rpx;
					font-size: 26rpx;
					color: #dddddd;
				}
			}

			.item_btn {
				width: 100rpx;
				background: $primary-b;
				height: 50rpx;
				border-radius: 10rpx;
				color: #fff;
				text-align: center;
				line-height: 50rpx;
			}
		}
	}

	.p_name {
		border: 1px solid #ccc;
		border-radius: 5px;
		padding: 10px;
		margin: 10rpx;
		display: flex;

		.img {
			width: 30%;
		}

		.good-year {
			color: #a6a6a6;
		}
	}

	.goods-num {
		display: flex;
		align-items: center;
		// border: 1px solid #ccc;
		border-radius: 10rpx;
		width: 300rpx;
		text-align: center;
		margin: 20rpx auto;

		.input {
			border: 1px solid #ccc;
			border-left: none;
			border-right: none;
			width: 40%;
			height: 80rpx;
			line-height: 80rpx;

		}

		.goods-sum {
			border: 1px solid #ccc;
			width: 30%;
			height: 80rpx;
			line-height: 80rpx;

			border-radius: 0 10rpx 10rpx 0;
		}

		.goods-sum:nth-child(1) {
			border-radius: 10rpx 0 0 10rpx;
		}

	}

	.swiper {
		padding: 10rpx;

		image {
			width: 100%;
		}
	}

	.address-title {
		display: flex;
		justify-content: space-between;
		padding: 20rpx 0;
		border-bottom: 1px solid #e9e9e9;
		margin-bottom: 20rpx;


		.icon_img {
			width: 40rpx;
		}
	}

	.remark {
		padding: 0rpx 0 20rpx;

		.remark-title {
			width: 90%;
			margin: 0 auto;
			padding: 10rpx 0;
			font-size: 28rpx;

		}

		.remark-text {
			border: 1px solid #cccccc;
			border-radius: 10rpx;
			width: 90%;
			margin: 0 auto;
			padding: 10rpx;
		}
	}

	.address_info {
		display: flex;

		.address_img {
			width: 200rpx;
			height: 200rpx;
			display: flex;
			align-items: center;

			.img {
				max-width: 100%;
				max-height: 100%;
			}
		}

		.address_info_detail {
			flex: 1;
			font-size: 24rpx;

			.postCode {
				padding: 5rpx 10rpx;
				border-radius: 5rpx;
				background: $primary-b;
				color: #fff;
				width: fit-content;
				margin-bottom: 20rpx;
			}

			.address_info_item {
				margin-bottom: 10rpx;
			}
		}
	}
</style>